package com.aternity.geolocations.transregions;

import com.aternity.common.geolocation.*;
import com.aternity.system.common.csv.CSVRWFactoryImpl;
import com.google.common.collect.Sets;
import org.apache.commons.codec.language.Metaphone;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.*;

/**
 * @author Daniel Orr
 */
public class TranslationManager {


    MaxMindRegions maxMindRegions = new MaxMindRegions();
    TableauNamesCache namesCache;

    Metaphone metaphone = new Metaphone();

    StringBuilder sb = new StringBuilder();

    public void loadData() {
        ReverseGeoCodingDataAccess reverseGeoCodingDataAccess= new ReverseGeoCodingDataAccess(CSVRWFactoryImpl.getInstance());
        
        Path countryGeoLocData = FileSystems.getDefault().getPath("X:\\j\\dnl-pg\\atn\\geolocating\\country.geom.csv");
        Path stateGeoLocData = FileSystems.getDefault().getPath("X:\\j\\dnl-pg\\atn\\geolocating\\state.geom.csv");
        reverseGeoCodingDataAccess.load(countryGeoLocData, stateGeoLocData);
        namesCache = reverseGeoCodingDataAccess.getNamesCache();

        try {
            maxMindRegions.load(new File("C:\\Users\\danielo\\Downloads\\region.csv"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void resolveRegionMatches() {
//        TextTreeTable ttt = new TextTreeTable();

        String[] isoCountries = Locale.getISOCountries();
        Set<String> countries = maxMindRegions.getIso3166AllCountries();

        for (String country : isoCountries) {
            System.out.println("________________________________________");
            System.out.println(country);
            System.out.println("________________________________________");
            // FIPS 2 name
            Map<String, String> maxMindRegionsCountryData = maxMindRegions.getCountryData(country);
            Set<String> maxMindStateNames = new HashSet<>(maxMindRegionsCountryData.values());
            Set<String> tableauStateNames = namesCache.getStatesInCountry(country);
            List<String> diff = new ArrayList<>(Sets.difference(maxMindStateNames, tableauStateNames));
            Collections.sort(diff);
            Sets.SetView<String> intersection = Sets.intersection(maxMindStateNames, tableauStateNames);

            sb = new StringBuilder();
            for (String s : intersection) {
                addLine(country, s, s);
            }
            System.out.println("\tmaxmind-tableau:");
            for (String s : diff) {
                System.out.print(s);
                String metaphone = findMetaphoneEquals(s, tableauStateNames);
                addLine(country, s, metaphone == null ? "":metaphone);
                System.out.println("\t|\t metaphones:   " + metaphone);
            }
            System.out.println("================");
            diff = new ArrayList<>(Sets.difference(tableauStateNames, maxMindStateNames));
            Collections.sort(diff);
            System.out.println("\ttableau-maxmind:");
            for (String s : diff) {
                System.out.print(s);
                System.out.println("\t|\t metaphones:   " +findMetaphoneEquals(s, maxMindStateNames));
            }
            System.out.println("*************************************");
            System.out.println(sb);
            System.out.println("*************************************");
            System.out.println();
        }
    }

    void addLine(String country, String maxmindState, String tableauState){
        sb.append(country);
        sb.append(",");
        sb.append(maxmindState);
        sb.append(",");
        sb.append(tableauState);
        sb.append("\n");
    }

    private String findMetaphoneEquals(String s, Set<String> set) {
        for (String s1 : set) {
            if (metaphone.isMetaphoneEqual(s, s1)) {
                return s1;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        TranslationManager translationManager = new TranslationManager();
        translationManager.loadData();
        translationManager.resolveRegionMatches();
    }
}
